package com.controller;

import com.result.CodeMsg;
import com.result.Result;
import com.service.MiaoshaUserService;
import com.service.UserService;
import com.util.ValidatorUtil;
import com.vo.LoginVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author lvcaihong
 * @dtae 2019/2/13 9:48
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    MiaoshaUserService miaoshaUserService;
    @Autowired
    UserService userService;

    @RequestMapping("/to_login")
    public String toLogin(){
       return "login";
    }

    @RequestMapping("/do_login")
    @ResponseBody
    public Result<Boolean> doLogin(@Valid LoginVo loginVo){
        logger.info(loginVo.toString());
        //参数校验(使用框架就不用这样了，直接在实体类中写相应的注解即可)
       /* String password = loginVo.getPassword();
        String mobile = loginVo.getMobile();

        if(StringUtils.isBlank(password)){
            return Result.error(CodeMsg.PASSWORD_EMPTY);
        }
        if(StringUtils.isBlank(mobile)){
            return Result.error(CodeMsg.MOBILE_EMPTY);
        }
        //判断手机号格式
        if(!ValidatorUtil.isMobile(mobile)){
            return Result.error(CodeMsg.MOBILE_ERROR);
        }*/
        //登录
        //异常情况直接抛出异常
        miaoshaUserService.login(loginVo);
//        if (codeMsg.getCode() == 0){
//            return Result.success(true);
//        }else{
//            return Result.error(codeMsg);
//        }
        return Result.success(true);
    }


   /* @RequestMapping("/do_login")

    @ResponseBody

    public Result<String> doLogin(HttpServletResponse response, @Valid LoginVo loginVo) {

        logger.info(loginVo.toString());

        //登录

        String token = miaoshaUserService.login(response, loginVo);

        return Result.success(token);

    }*/



}

